//
//  TJBuyHeaderView.m
//  Teleport
//
//  Created by hc-161 on 2020/6/12.
//  Copyright © 2020 xtjun. All rights reserved.
//

#import "TJBuyHeaderView.h"
#import "UIButton+LXMImagePosition.h"


@interface TJBuyHeaderView()<JXCategoryViewDelegate>

@property (weak, nonatomic) UILabel *buyCountLabel;
@property (strong, nonatomic) NSArray *coinNames;
@end

@implementation TJBuyHeaderView


- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubView];
    }
    return self;
}

- (void)addSubView {
    
    self.backgroundColor = UIColor.whiteColor;
    
    UIView *topView = [UIView tj_addToView:self];
    topView.backgroundColor = UIColor.clearColor;
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.offset(0);
        make.height.offset(40);
    }];
    
    /**币子选择按钮 **/
    UIButton *coinNameButton = [UIButton buttonWithSuperView:topView font:UIFont_Px(15) color:UIColor_Hex(0x262626) title:@"CNY/BTC"];
    [coinNameButton setImage:[UIImage imageNamed:@"down_icon"] forState:UIControlStateNormal];
    [coinNameButton setImage:[UIImage imageNamed:@"down_icon"] forState:UIControlStateHighlighted];
    [coinNameButton setImagePosition:LXMImagePositionRight spacing:3];
    [coinNameButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [coinNameButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(160);
        make.left.offset(16);
        make.height.offset(34);
        make.centerY.offset(0);
    }];
    /**订单按钮 **/
    UIButton *orderButton = [UIButton buttonWithSuperView:topView font:UIFont_Px(12) color:UIColor_Hex(0x3781E7) title:@"订单".localizedString];
    [orderButton setImage:[UIImage imageNamed:@"orderList_icon"] forState:UIControlStateNormal];
    [orderButton setImage:[UIImage imageNamed:@"orderList_icon"] forState:UIControlStateHighlighted];
    [orderButton setImagePosition:LXMImagePositionLeft spacing:3];
    [orderButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    [orderButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(60);
        make.right.offset(-16);
        make.height.equalTo(coinNameButton);
        make.centerY.offset(0);
    }];
    
    UIView *tj_contentView = [UIView tj_addToView:self];
    tj_contentView.layer.shadowOffset = CGSizeMake(0, 3);
    tj_contentView.backgroundColor = UIColor.whiteColor;
    tj_contentView.layer.shadowColor = [UIColor.blackColor colorWithAlphaComponent:0.4].CGColor;
    tj_contentView.layer.shadowRadius = 5;
    tj_contentView.layer.shadowOpacity = 0.3;
    tj_contentView.layer.cornerRadius = 5;
    tj_contentView.layer.masksToBounds = NO;
    [tj_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topView.mas_bottom).offset(5);
        make.left.offset(16);
        make.right.offset(-16);
        make.height.offset(196);
    }];
    
    UILabel *buyCountLabel = [UILabel lableWithSuperView:tj_contentView font:UIFont_Px(14) color:UIColor_Hex(0x262626) title:@"购买金额".localizedString textAlignment:NSTextAlignmentLeft];
    [buyCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(16);
        make.top.offset(16);
    }];
    
    /**￥label **/
    UILabel *unitLabel = [UILabel lableWithSuperView:tj_contentView font:MediumFont(18) color:UIColor_Hex(0x262626) title:@"￥" textAlignment:NSTextAlignmentLeft];
    [unitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(16);
        make.top.equalTo(buyCountLabel.mas_bottom).offset(16);
        make.width.offset(16);
    }];
    
    /**购买数量输入框 **/
    UITextField *numberTextField = [UITextField tj_addToView:tj_contentView];
    numberTextField.placeholder = @"100元起".localizedString;
    numberTextField.font = UIFont_Px(15);
    numberTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    numberTextField.keyboardType = UIKeyboardTypeDecimalPad;
    [numberTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(unitLabel.mas_right).offset(4);
        make.centerY.equalTo(unitLabel);
        make.height.offset(30);
        make.right.offset(-16);
    }];
    
    UIView *lineView = [UIView tj_addToView:tj_contentView];
    lineView.backgroundColor = UIColor_Hex(0xE5E5E5);
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(unitLabel);
        make.right.offset(-16);
        make.height.offset(1);
        make.top.equalTo(numberTextField.mas_bottom).offset(2);
    }];
    
    /**单价约label **/
    UILabel *priceLabel = [UILabel lableWithSuperView:tj_contentView font:UIFont_Px(11) color:UIColor_Hex(0x656565) title:@"" textAlignment:NSTextAlignmentLeft];
    [priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(16);
        make.top.equalTo(lineView.mas_bottom).offset(4);
    }];
    
    /**金额 数量按钮 **/
    UIButton *buyWayButton = [UIButton buttonWithSuperView:tj_contentView font:UIFont_Px(12) color:UIColor_Hex(0x3781E7) title:@"按数量购买".localizedString];
    [buyWayButton setImage:[UIImage imageNamed:@"conversion_icon"] forState:UIControlStateNormal];
    [buyWayButton setImage:[UIImage imageNamed:@"conversion_icon"] forState:UIControlStateHighlighted];
    [buyWayButton setTitle:@"按金额购买".localizedString forState:UIControlStateSelected];
    [buyWayButton addTarget:self action:@selector(didClickBuyWayButton:) forControlEvents:UIControlEventTouchUpInside];
    [buyWayButton setImagePosition:LXMImagePositionLeft spacing:3];
    [buyWayButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    [buyWayButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(150);
        make.right.offset(-16);
        make.height.offset(30);
        make.centerY.equalTo(priceLabel);
    }];
    
    /**购买按钮 **/
    UIButton *buyButton = [UIButton buttonWithSuperView:tj_contentView font:UIFont_Px(18) color:UIColor.whiteColor title:@"购买".localizedString];
    buyButton.backgroundColor = UIColor.tj_mainColor;
    [buyButton shearRoundedCornersWithRadiu:22];
    [buyButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-22);
        make.left.offset(22);
        make.height.offset(44);
        make.centerX.offset(0);
        make.top.equalTo(priceLabel.mas_bottom).offset(25);
    }];
    
    UIButton *freeButton = [UIButton buttonWithSuperView:tj_contentView font:UIFont_Px(11) color:UIColor_Hex(0x3781E7) title:@"交易0手续费".localizedString];
    [freeButton setImage:[UIImage imageNamed:@"tip_icon"] forState:UIControlStateNormal];
    [freeButton setImage:[UIImage imageNamed:@"tip_icon"] forState:UIControlStateHighlighted];
    [freeButton setImagePosition:LXMImagePositionLeft spacing:3];
    [freeButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [freeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(buyButton);
        make.height.offset(20);
        make.width.offset(200);
        make.top.equalTo(buyButton.mas_bottom).offset(4);
    }];
    
    UIView *bottomView = [UIView tj_addToView:self];
    bottomView.backgroundColor = UIColor.clearColor;
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.top.equalTo(tj_contentView.mas_bottom);
    }];
    
    /** 筛选按钮**/
    UIButton *screeningButton = [UIButton buttonWithSuperView:bottomView font:UIFont_Px(12) color:UIColor_Hex(0x3781E7) title:@"筛选".localizedString];
    [screeningButton setImage:[UIImage imageNamed:@"screening_icon"] forState:UIControlStateNormal];
    [screeningButton setImage:[UIImage imageNamed:@"screening_icon"] forState:UIControlStateHighlighted];
    [screeningButton setImagePosition:LXMImagePositionLeft spacing:3];
    [screeningButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    [screeningButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(60);
        make.right.offset(-16);
        make.height.equalTo(coinNameButton);
        make.centerY.offset(0);
    }];
    
    _screeningButton = screeningButton;
    _numberTextField = numberTextField;
    _buyWayButton = buyWayButton;
    
    _buyButton = buyButton;
    _orderButton = orderButton;
    _coinNameButton = coinNameButton;
    _buyCountLabel = buyCountLabel;
    _numberTextField = numberTextField;
    _priceLabel = priceLabel;
    _unitLabel = unitLabel;
    [self setNeedsLayout];
    [self layoutIfNeeded];
    
    
    
#pragma mark ---------------选择器------------------
    _coinNames = @[@"BTC", @"USDT"];
    
    JXCategoryIndicatorLineView *indicatorLineView = [[JXCategoryIndicatorLineView alloc] init];
    indicatorLineView.indicatorHeight = 2;
    indicatorLineView.indicatorColor = UIColor.blackColor;
    indicatorLineView.verticalMargin = 5;
    
    self.categoryView.titles = _coinNames;
    self.categoryView.titleFont = UIFont_Px(15);
    self.categoryView.titleColor = UIColor_Hex(0x262626);
    self.categoryView.titleSelectedColor = UIColor_Hex(0x262626);
    self.categoryView.selectedAnimationEnabled = NO;
    self.categoryView.delegate = self;
    self.categoryView.titleLabelZoomEnabled = NO;
    self.categoryView.cellSpacing = 15;
    self.categoryView.frame = CGRectMake(0, 0, 122, 40);
    self.categoryView.indicators = @[indicatorLineView];
    self.categoryView.titleColorGradientEnabled = YES;
    [bottomView addSubview:self.categoryView];
    
    
    
}

- (void)setSeleteType:(int)seleteType {
    _seleteType = seleteType;
    
    if (seleteType == 0) {
        [_buyButton setTitle:@"购买".localizedString forState:UIControlStateNormal];
        _buyButton.backgroundColor = UIColor.tj_mainColor;
        
        if (_buyButton.selected) {
            _numberTextField.placeholder = @"请输入购买数量".localizedString;
            _buyCountLabel.text = @"购买数量".localizedString;
            
            [_unitLabel mas_updateConstraints:^(MASConstraintMaker *make) {
                make.width.offset(0);
            }];
        } else {
            _buyCountLabel.text = @"购买金额".localizedString;
            _numberTextField.placeholder = @"100元起".localizedString;
            [_unitLabel mas_updateConstraints:^(MASConstraintMaker *make) {
                make.width.offset(16);
            }];
        }
        
        [_buyWayButton setTitle:@"按数量购买".localizedString forState:UIControlStateNormal];
        [_buyWayButton setTitle:@"按金额购买".localizedString forState:UIControlStateSelected];
        
        
    } else {
        [_buyButton setTitle:@"出售".localizedString forState:UIControlStateNormal];
        _buyButton.backgroundColor = UIColor_Hex(0xDA2629);
        [_buyWayButton setTitle:@"按数量出售".localizedString forState:UIControlStateNormal];
        [_buyWayButton setTitle:@"按金额出售".localizedString forState:UIControlStateSelected];
        
        if (_buyButton.selected) {
            _numberTextField.placeholder = @"请输入出售数量".localizedString;
            _buyCountLabel.text = @"出售数量".localizedString;
            [_unitLabel mas_updateConstraints:^(MASConstraintMaker *make) {
                make.width.offset(0);
            }];
        } else {
            _buyCountLabel.text = @"出售金额".localizedString;
            _numberTextField.placeholder = @"100元起".localizedString;
            [_unitLabel mas_updateConstraints:^(MASConstraintMaker *make) {
                make.width.offset(16);
            }];
        }
    }
    
}

- (void)didClickBuyWayButton:(UIButton *)button {
    button.selected = !button.selected;
    
    
    if (_seleteType == 0) {
        
        if (button.selected) {
            _buyCountLabel.text = @"购买数量".localizedString;
            _numberTextField.placeholder = @"请输入购买数量".localizedString;
            [_unitLabel mas_updateConstraints:^(MASConstraintMaker *make) {
                make.width.offset(0);
            }];
            
        } else {
            _buyCountLabel.text = @"购买金额".localizedString;
            _numberTextField.placeholder = @"100元起".localizedString;
            [_unitLabel mas_updateConstraints:^(MASConstraintMaker *make) {
                make.width.offset(16);
            }];
        }
    } else {
        if (button.selected) {
            _numberTextField.placeholder = @"请输入出售数量".localizedString;
            _buyCountLabel.text = @"出售数量".localizedString;
            [_unitLabel mas_updateConstraints:^(MASConstraintMaker *make) {
                make.width.offset(0);
            }];
            
        } else {
            _numberTextField.placeholder = @"100元起".localizedString;
            _buyCountLabel.text = @"出售金额".localizedString;
            [_unitLabel mas_updateConstraints:^(MASConstraintMaker *make) {
                make.width.offset(16);
            }];
            
        }
    }
    
}

- (JXCategoryTitleView *)categoryView {
    if (_categoryView == nil) {
        _categoryView = [[JXCategoryTitleView alloc] init];
    }
    return _categoryView;
}

#pragma mark ---------------JXCategoryViewDelegate------------------
- (void)categoryView:(JXCategoryBaseView *)categoryView didSelectedItemAtIndex:(NSInteger)index {
    if (_didClickCoinNameBlock) {
        _didClickCoinNameBlock(_coinNames[index], index);
    }
}


@end
